package com.foreknow.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.foreknow.dao.SchedulingMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.Rule;
import com.foreknow.entity.Scheduling;
import com.foreknow.entity.User;
import com.foreknow.service.SchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SchedulingServiceImpl implements SchedulingService {
    @Autowired
    private SchedulingMapper schedulingMapper;

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @Override
    public List<Scheduling> All(String query) {
        List<Scheduling> list = schedulingMapper.selectAll(query);
        return list;
    }

    /**
     * 根据日期查询值班人员
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Scheduling> getSomeByDate(String startTime, String endTime) {
        return schedulingMapper.selectSome(startTime, endTime);
    }

    /**
     * 根据科室以及挂号等级查询医生
     *
     * @return
     */
    @Override
    public List<User> getUser(String dept, String level) {
        return schedulingMapper.selectUserDoctor(dept, level);
    }

    @Override
    public List<Department> getAllDepart() {
        return schedulingMapper.selectDepartAll();
    }

    /**
     * 根据name查询用户id
     *
     * @param name
     * @return
     */
    @Override
    public User getUserIdByName(String name) {
        return schedulingMapper.selectIdByName(name);
    }

    /**
     * 添加值班规则
     *
     * @param
     * @return
     */
    @Override
    public int addRule(Rule rule) {
        return schedulingMapper.insertRule(rule);
    }

    /**
     * 选取规则生成排班计划
     *
     * @param list
     * @return
     */
    @Override
    public int addscheduling(List<Scheduling> list) {
        return schedulingMapper.insert(list);
    }

    /**
     * 将Object转换成List集合
     *
     * @param obj
     * @param clazz
     * @return
     */
    public List<User> castList(Object obj, Class<User> clazz) {
        List<User> result = new ArrayList();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

    /**
     * 日期增加一天
     *
     * @param c
     * @return
     */
    public long get_D_Plaus_1(Calendar c) {
        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
        return c.getTimeInMillis();
    }

    /**
     * 获取两个日期之间的所有日期(包含开始时间和结束时间)
     *
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @Override
    public List<String> getTimesBetweenDates(String startTime, String endTime) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date start = dateFormat.parse(startTime);
        Date end = dateFormat.parse(endTime);
        //如果时间传反了
        long time1 = start.getTime();
        long time2 = end.getTime();
        Date midTime = new Date();
        if (time1 > time2) {
            midTime = start;
            start = end;
            end = midTime;
        }
        List<String> result = new ArrayList<String>();
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(start);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(end);
        endCal.add(Calendar.DATE, +1);// 日期+1,将结束日期放入结果集

        while (startCal.before(endCal)) {
            String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(startCal.getTime());
            result.add(dateStr);
            startCal.add(Calendar.DAY_OF_YEAR, 1);
        }

        return result;
    }

    /**
     * 根据UserId与RuleName查找RuleId
     *
     * @param week
     * @param id
     * @return
     */
    @Override
    public int getRuleId(String week, Integer id) {
        int RuleId = schedulingMapper.selectRuleId(week, id);
        return RuleId;
    }


}